﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MMDemo
{
    public partial class BinTreeNewForm : Form
    {
        public BinTreeNewForm()
        {
            InitializeComponent();
        }

        private void InputStrtext_TextChanged(object sender, EventArgs e)
        {
            SysStrtext.Text = InputStrtext.Text.Replace(" ", "#").ToUpper();            
        }
        private int num = 0;//二叉树的结点数量
        private const int MAX_NUM = 16;//二叉树最大结点数
        private BinTreeNode root;
        private string ramstrdata;
        private void RamCrebtn_Click(object sender, EventArgs e)
        {
            root = null;
            ramstrdata = "";
            num = 0;
            RandCreateTree(ref root, 0);
            ConRoot_Str(ref root,ref ramstrdata);
            SysStrtext.Text = ramstrdata;
            InputStrtext.Text = ramstrdata.Replace("#", " ");
        }

        Random ran = new Random();
        private void RandCreateTree(ref BinTreeNode root, int n)
        {
            int IsPresence = ran.Next(100 - n * 5);
            if (IsPresence == 0)
            {
                root = null;
            }
            else if (num < MAX_NUM)
            {
                root = new BinTreeNode();
                ++num;
                root.data = ((char)(ran.Next(26) + 65)).ToString();
                if (ran.Next(2) == 1 && n < 3)
                {
                    RandCreateTree(ref root.lChild, n + 1);
                }
                if (ran.Next(2) == 1 && n < 3)
                {
                    RandCreateTree(ref root.rChild, n + 1);
                }

            }

        }


        private void ConRoot_Str(ref BinTreeNode root,ref string tempStr)
        {
            if (root==null)
            {
                tempStr += "#";
                return;
            }
            tempStr += root.data.ToString();
            ConRoot_Str(ref root.lChild, ref tempStr);
            ConRoot_Str(ref root.rChild, ref tempStr);        
        }
        private BinTreeNode btn;
        private string curstr;
        private void ConStr_Root(ref BinTreeNode btn,ref string str)
        {
            if (str=="")
            {
                btn = null;
                return;
            }
            if ( str[0].ToString()==""||str[0].ToString()=="#")
            {
                btn = null;
                str = str.Remove(0, 1);
                return;
            }
            btn = new BinTreeNode();
            btn.data = str[0].ToString();
            str = str.Remove(0, 1);
            ConStr_Root(ref btn.lChild,ref str);
            ConStr_Root(ref btn.rChild,ref str);
        }
        private void Okbtn_Click(object sender, EventArgs e)
        {
            curstr = SysStrtext.Text;
            ConStr_Root(ref btn, ref curstr);
            if (curstr!="")
            {
                MessageBox.Show("该先序遍历字符序列不能生成二叉树!");
                return;
            }
            string temp="";
            ConRoot_Str(ref btn,ref temp);
            curstr = SysStrtext.Text;
            if (curstr!= temp)
            {
                MessageBox.Show("系统将自动为您补充空子树标志!");
                SysStrtext.Text = temp;
            }
            GlobalBinTree.pretreedata = SysStrtext.Text;
            GlobalBinTree.treeroot = btn;
            GlobalBinTree.treeroot = root;
            PublicDataClass.IsInit = true;
            this.Close();
        }

        protected override void WndProc(ref   Message m)
        {
            const int WM_SYSCOMMAND = 0x0112;
            const int SC_CLOSE = 0xF060;
            if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_CLOSE)
            {
                PublicDataClass.IsInit = false;
                this.Close();
                return;
            }
            base.WndProc(ref m);
        }
    }
}
